﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface IMainMenu
  {

    event MenuEvent OnSettingsClicked;
    event MenuEvent OnHelpClicked;
    event MenuEvent OnCreditsClicked;
    event MenuEvent OnHighScoresClicked;

  }


  public class MainMenu : BaseMenu, IMainMenu
  {

    Button settingsBtn, creditsBtn, helpBtn; //highScoresBtn;
    SpriteFont largeFont;
    public int colorBtnTimer, direction = 1, counter = 0;
    SpriteBatch spriteBatch;
    Texture2D Eleven, ElevenBig;
    

    public MainMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = game.Content.Load<Texture2D>(@"Images//Menu//MainMenuCard");
      bounds = new Rectangle(90, 30, 220, 331);
      game.Services.AddService(typeof(IMainMenu), this);
    }

    protected override void Dispose(bool disposing) 
    {
      Game.Services.RemoveService(typeof(IMainMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      spriteBatch = Game.GetContext().SpriteBatch;

      Eleven = Game.Content.Load<Texture2D>(@"Images//Menu//AddBackgroundEleven");
      ElevenBig = Game.Content.Load<Texture2D>(@"Images//Menu//AddBackgroundElevenBig");
      

      //Font
      largeFont = Game.Content.Load<SpriteFont>("baveuse1_font");

      // Load all buttons
      int strLen = (int)largeFont.MeasureString(Locale.Strings.Settings).X;
      settingsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 10, 76, strLen + 20, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Settings, Color.Black, 0.9f);
      AddButton(settingsBtn, () => 
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        settingsBtn.textColor = Color.Red;
        colorBtnTimer = 5;
       });

      strLen = (int)largeFont.MeasureString(Locale.Strings.Help).X;
      helpBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 20, 155, strLen + 40, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Help, Color.Black, 0.9f);
      AddButton(helpBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        helpBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });

      //strLen = (int)largeFont.MeasureString(Locale.Strings.HighScores.Replace(" ", "\n")).X;
      //highScoresBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 2, 215, strLen, 25), Color.Transparent, "baveuse1_font", Locale.Strings.HighScores.Replace(" ", "\n"), Color.Black, 0.8f);
      //AddButton(highScoresBtn, () =>
      //{
      //  var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
      //  SoundManager.PlaySound(MenuSounds.ClickCard);

      //  highScoresBtn.textColor = Color.Red;
      //  colorBtnTimer = 5;
      //});


      strLen = (int)largeFont.MeasureString(Locale.Strings.Credits).X;
      creditsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 20, 237, strLen + 40, 70), Color.Transparent, "baveuse1_font", Locale.Strings.Credits, Color.Black, 0.9f);
      AddButton(creditsBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        creditsBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });

      base.Initialize();
    }

    public override void Update(GameTime gameTime)
    {

      if (settingsBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnSettingsClicked != null)
            OnSettingsClicked();
        }
      }

      if (creditsBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnCreditsClicked != null)
            OnCreditsClicked();
        }
      }

      if (helpBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnHelpClicked != null)
            OnHelpClicked();
        }
      }


      
      
      //if (highScoresBtn.textColor == Color.Red)
      //{
      //  colorBtnTimer--;
      //  if (colorBtnTimer < 0)
      //  {
      //    if (OnHighScoresClicked != null)
      //      OnHighScoresClicked();
      //  }
      //}
      
      base.Update(gameTime);
    }

    private Vector2 input_service_OnTap(Vector2 tapPosition)
    {
      //point = new Point((int)tapPosition.X, (int)tapPosition.Y);

      //return point;
      return tapPosition;
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);
      
        spriteBatch.Begin();

        spriteBatch.Draw(Eleven, new Rectangle(650, 0, 150, 100), Color.White);
        spriteBatch.Draw(ElevenBig, new Rectangle(0, 0, 380, 60), Color.White);
        
        spriteBatch.End();

    }


    public event MenuEvent OnSettingsClicked;
    public event MenuEvent OnHelpClicked;
    public event MenuEvent OnCreditsClicked;
    public event MenuEvent OnHighScoresClicked;
  }
}
